#include "myqwtplot.h"

MyQwtPlot::MyQwtPlot(QWidget *parent):
    QwtPlot(parent)
{
    //this->plotType=type;

    //QFont

        setAutoReplot( false );
        //setStyleSheet("font:9pt Bitstream Charter;background-color: rgb(173, 175, 178);color:rgb(0,0,0);");//9
        //titleLabel()->setStyleSheet("font:75 12pt Loma;");
        // canvas

        canvas =new QwtPlotCanvas();
        //canvas->setFrameStyle(QFrame::NoFrame);//QFrame::Box|QFrame::Plain QFrame::NoFrame
        canvas->setStyleSheet("border-top: 1px solid black;border-right: 1px solid black;background-color: white;");//border: 2px solid rgb(0, 85, 127);
        setCanvas( canvas );
        //canvas->setCursor();QColor(227,241,254)
        //一个选择器，十字线，以xBottom和yLeft坐标

        picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
                                   QwtPlotPicker:: RectRubberBand, QwtPicker::AlwaysOff,
                                   canvas);
        picker->setStateMachine(new QwtPickerClickPointMachine());//拖拽点起作用
        picker->setRubberBandPen(QPen(QColor(Qt::red)));
        picker->setTrackerPen(QColor(Qt::black));//

        // grid
        grid = new QwtPlotGrid;
        grid->enableXMin( true );
        grid->enableYMin( true );
        grid->setMajorPen( Qt::gray, 0, Qt::DotLine );
        grid->setMinorPen( Qt::lightGray, 0, Qt::DotLine );
        grid->attach( this );
        //QwtScaleDraw
        plotLayout()->setAlignCanvasToScales( true );
        //pPlot->plotLayout()->setCanvasMargin(-1);//能去除线宽的影响，但轴末端对不齐
        for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ )
        {
            axisWidget( axis )->setMargin( 0 );
        }

        zoomer = new QwtPlotZoomer( canvas );                     //选择图形局部放大
        connect(zoomer,SIGNAL(zoomed(const QRectF&)),this, SLOT(OnZoomer(const QRectF&)));
        zoomer->setRubberBandPen( QColor( Qt::blue ) ); //勾选扩大区域边界颜色
        zoomer->setMousePattern( QwtEventPattern::MouseSelect2, Qt::RightButton);//, Qt::ControlModifier
        zoomer->setMousePattern( QwtEventPattern::MouseSelect1, Qt::LeftButton );//, Qt::ControlModifier
        //zoomer->setMousePattern( QwtEventPattern::MouseSelect3, Qt::RightButton );
        zoomer->setMaxStackDepth(5);

        setAxisAutoScale(QwtPlot::xBottom,true);
        setAxisAutoScale(QwtPlot::yLeft,true);
        //setAxisScale( QwtPlot::xBottom, 0,40);

//        enableAxis (QwtPlot::yRight, true);
//        setAxisScaleEngine(QwtPlot::yRight, NULL);
//        enableAxis (QwtPlot::xTop, true);
}

MyQwtPlot::~MyQwtPlot()
{

}

void MyQwtPlot::RecoverXY()//
{
    setAxisAutoScale(QwtPlot::xBottom,true);
    setAxisAutoScale(QwtPlot::yLeft,true);
}

void MyQwtPlot::OnZoomer(const QRectF &rect )
{
    if (zoomer->zoomRectIndex()==0)
    {
        setAxisAutoScale(QwtPlot::xBottom,true);
        setAxisAutoScale(QwtPlot::yLeft,true);
    }
    replot();
}
